package cn.gdcp.myapplication;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;


public class MyAdapter extends BaseAdapter {
                   private ArrayList<Student> arrayList;
                   private Context context;
                   private IOnDelListener delListener;

                   public  MyAdapter(Context context, ArrayList<Student> arrayList,IOnDelListener listener) {
                       this.arrayList = arrayList;
                       this.context = context;
                       this.delListener=listener;
                   }
       @Override
       public int getCount() {
           return arrayList.size();
       }

       @Override
       public Object getItem(int i) {
           return arrayList.get(i);
       }

       @Override
       public long getItemId(int i) {
           return i;
       }

       @SuppressLint("ViewHolder")
       @Override
       public View getView( final  int i, View view, ViewGroup viewGroup) {
              if (view==null) {
                  LayoutInflater layoutInflater = LayoutInflater.from(context);
                  view = layoutInflater.inflate(R.layout.ada_stu_list,
                          viewGroup, false);


                  Holder holder = new Holder();
                  holder.ada_img = view.findViewById(R.id.ada_img);
                  holder.ada_name = view.findViewById(R.id.ada_name);
                  holder.ada_age = view.findViewById(R.id.ada_age);
                  holder.del_stu = view.findViewById(R.id.del_stu);
                  view.setTag(holder);

              }
                       Holder holder=(Holder)view.getTag();
                       Student stu=arrayList.get(i);

                     holder.ada_img.setImageResource(stu.getImgid());
                 holder.ada_name.setText(stu.getName());
                 holder.ada_age.setText(String.valueOf(stu.getAge()));
                 holder.del_stu.setOnClickListener(new View.OnClickListener() {
                     @Override
                     public void onClick(View view) {
                        delListener.del(i);
                     }
                 });

           return view;
       }
   }
